package com.heisenhub.common.oauth.controller;

import com.heisenhub.common.oauth.annotation.ApiResourceOpen;
import com.heisenhub.common.oauth.enums.RESTAPI;
import com.heisenhub.common.oauth.inter.ThreePushedDataReceiver;
import com.heisnenhub.common.module.inter.ThreeResult;
import com.heisnenhub.common.module.utils.Api;
import com.heisnenhub.common.module.utils.GsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
public class BaseController {

    @Autowired(required = false)
    private Map<String, ThreePushedDataReceiver> receivers = new HashMap<>();


    /**
     * k8s 就绪探针接口
     * @return
     */
    @GetMapping("/ready")
    @ApiResourceOpen
    public ThreeResult ready() {
       return  Api.ok();
    }

    /**
     * @Description:  子系统推送数据
     * @Date: 2023/5/6 14:46
     * @param pustdata
     * @param request
     */
    @PostMapping("/thirdNotify/{pustdata}")
    @ApiResourceOpen
    public ThreeResult thirdDeviceDataReceive(@PathVariable String pustdata, HttpServletRequest request) {
        try {
            String msgData = "";
            if (request.getContentType().contains("application/json")) {
                msgData = getBodytxt(request);
            } else {
                Map<String, String[]> map = request.getParameterMap();
                msgData = GsonUtils.toJson(map);
            }

            if (StringUtils.isNotBlank(msgData)) {
                ThreePushedDataReceiver threePushedDataReceiver= receivers.get(pustdata);
                if(threePushedDataReceiver !=null){
                    return threePushedDataReceiver.threePushDataReceive(msgData);
                }
            }
            return Api.ok();
        } catch (Exception e) {
            return Api.fail();
        }
    }


    /**
     *  流变成数据  变成stream
     * @param request
     * @return
     */
    private String getBodytxt(HttpServletRequest request) {
        try (InputStream inStream = request.getInputStream(); ByteArrayOutputStream outStream = new ByteArrayOutputStream()) {
            int _buffer_size = 1024;
            byte[] tempBytes = new byte[_buffer_size];
            int count = -1;
            while ((count = inStream.read(tempBytes, 0, _buffer_size)) != -1) {
                outStream.write(tempBytes, 0, count);
            }
            tempBytes = null;
            String result = new String(outStream.toByteArray(), "UTF-8");
            return result;
        } catch (IOException e) {
            log.error("Read data from request error", e);
        }
        return null;
    }
}
